package com.boyuan.huijiaoyu;

import java.util.ArrayList;
import java.util.List;

import com.example.contactfragment.ContactGroupService;
import com.example.newfragment.DetailPerson;
import com.magicsoft.app.wcf.listener.GetMultilParamsListener;
import com.magicsoft.app.wcf.listener.GetOneRecordListener;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class DelGroupMemberActivity extends Activity implements OnClickListener {

	private ListView listview1;
	private ArrayList<String> memberlist = new ArrayList<String>();
	private ArrayList<String> deletelist = new ArrayList<String>();
	private MyAdapter adapter;
	private ContactGroupService contactgroupService;
	String id;
	private String groupid;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_del_group_member);
		initUI();
	}

	private void initUI() {
		SharedPreferences sp = getSharedPreferences("a.xml", 0);
		id = sp.getString("id", "");
		Intent intent = getIntent();
		groupid = intent.getStringExtra("groupid");
		memberlist = intent.getStringArrayListExtra("memberlist");
		findViewById(R.id.lay_back).setOnClickListener(this);
		findViewById(R.id.lay_save).setOnClickListener(this);
		listview1 = (ListView) findViewById(R.id.listView1);
		adapter = new MyAdapter();
		listview1.setAdapter(adapter);
		listview1.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int arg2,
					long arg3) {
				CheckBox cb1 = (CheckBox) view.findViewById(R.id.checkBox1);
				cb1.toggle();
			}
		});
	}

	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return memberlist.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View inflate = getLayoutInflater().inflate(R.layout.list_friend,
					null);
			final TextView tv_user = (TextView) inflate
					.findViewById(R.id.tvname);
			CheckBox cb = (CheckBox) inflate.findViewById(R.id.checkBox1);
			String member = memberlist.get(position);
			tv_user.setText(member);
			cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					if (isChecked) {
						deletelist.add(tv_user.getText().toString().trim());
					} else {
						deletelist.remove(tv_user.getText().toString().trim());
					}

				}
			});
			return inflate;
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.del_group_member, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lay_back:
			finish();
			break;
		case R.id.lay_save:
			delMember();
			break;
		default:
			break;
		}
	}

	private void delMember() {
		if (contactgroupService == null) {
			contactgroupService = new ContactGroupService(this);
		}
		StringBuffer sb = new StringBuffer();
		String usernames = "";
		int size = deletelist.size();
		for (int i = 0; i < size; i++) {
			sb.append(deletelist.get(i) + ",");
		}
		usernames = sb.substring(0, sb.length() - 1);
		contactgroupService.delGroupMember(size+"", groupid, usernames,
				new GetOneRecordListener<List<DeleMember>>() {

					@Override
					public void onFinish(List<DeleMember> data) {
						Intent intent = new Intent();
						setResult(4, intent);
						finish();
					}

					@Override
					public void onFailed(String Message) {

					}

				}, new GetMultilParamsListener() {

					@Override
					public void onFinish(Object... params) {
						Intent intent = new Intent();
						setResult(4, intent);
						finish();
					}

					@Override
					public void onFailed(String Message) {
						// TODO Auto-generated method stub

					}
				});
	}

}
